package io.unicorn.modules.water.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import io.unicorn.common.excelutils.ExcelListener;
import io.unicorn.common.exception.RRException;
import io.unicorn.common.utils.ExcelUtils;
import io.unicorn.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.unicorn.modules.water.entity.MeterArchivesEntity;
import io.unicorn.modules.water.service.MeterArchivesService;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * ${comments}
 *
 * @author LNCG
 * @email LNCG@USTL.EDU.CN
 * @date 2022-03-10 10:58:55
 */
@RestController
@RequestMapping("water/meterarchives")
public class MeterArchivesController {
    @Autowired
    private MeterArchivesService meterArchivesService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("water:meterarchives:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = meterArchivesService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{unkey}")
    @RequiresPermissions("water:meterarchives:info")
    public R info(@PathVariable("unkey") String unkey){
        MeterArchivesEntity meterArchives = meterArchivesService.getById(unkey);

        return R.ok().put("meterArchives", meterArchives);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("water:meterarchives:save")
    public R save(@RequestBody MeterArchivesEntity meterArchives){
        meterArchivesService.save(meterArchives);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("water:meterarchives:update")
    public R update(@RequestBody MeterArchivesEntity meterArchives){
        ValidatorUtils.validateEntity(meterArchives);
        meterArchivesService.updateById(meterArchives);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("water:meterarchives:delete")
    public R delete(@RequestBody String[] unkeys){
        meterArchivesService.removeByIds(Arrays.asList(unkeys));

        return R.ok();
    }

    @RequestMapping("/export")
    @RequiresPermissions("water:meterarchives:list")
    public void export(@RequestParam Map<String, Object> params,
                       HttpServletRequest request,
                       HttpServletResponse repsonse){
        JSONObject jsonObject = JSONObject.parseObject((String)params.get("params"));
        PageUtils page = meterArchivesService.queryPage(jsonObject);
        List<?> listEntity = page.getList();

        ExcelUtils.exportXlsx(
                repsonse,
                "export",
                "io.unicorn.modules.water.entity.MeterArchivesEntity",
                listEntity);
    }

    //  Excel导入数据到数据库
    @PostMapping("/importExcel")
    @RequiresPermissions("water:meterarchives:update")
    public R importExcel(@RequestParam("myfile") MultipartFile myFile){
        if (myFile.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        String fileName = myFile.getOriginalFilename();//  获取文件名
        //String suffix = myFile.getOriginalFilename().substring(myFile.getOriginalFilename().lastIndexOf("."));
        //Integer nums = meterArchivesService.importExcel(myFile);
        try {
            EasyExcel.read(myFile.getInputStream(), MeterArchivesEntity.class, new ExcelListener(p -> {
                //保存逻辑（可封装一个普通的sevice）p-为我们执行函数时传入的list
                //sendAddressService.importExcel(p);
                meterArchivesService.saveOrUpdateBatch(p);
            })).sheet().doRead();
        }catch (IOException e) {
            e.printStackTrace();
        }

        return R.ok().put("url",fileName);
    }

}
